//Canvas 库测试
import 'dart:math';
import 'dart:ui';
import 'dart:ui' as ui; // 引入 dart:ui 的 ui 模块
import 'package:flutter/material.dart';
import 'package:flutter_anim_test/utils/canvas_extends.dart';
import 'package:flutter_anim_test/utils/image_utils.dart';
import 'package:flutter_svg/flutter_svg.dart';

import '../generated/assets.dart';

class CanvasTestPage extends StatefulWidget {
  const CanvasTestPage({super.key, required this.title});

  final String title;

  @override
  State<CanvasTestPage> createState() => _CanvasTestPageState();
}

class _CanvasTestPageState extends State<CanvasTestPage> {
  bool isOpen = false;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  void _incrementCounter() {
    setState(() {
      isOpen = !isOpen;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          spacing: 10,
          children: <Widget>[
            Container(
              color: Colors.red,
              width: 54,
              height: 34,
            ),
            FutureBuilder(
                // future: ImageUtils.loadSvgImage(context, Assets.svgIcBike, width: 54, height: 34),
                future: ImageUtils.loadImage(Assets.imgBike),
                builder: (context, snapshot) {
                  return CustomPaint(
                    painter: MyPainter(context: context, image: snapshot.data),
                    size: Size(double.infinity, 200),
                  );
                }),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

class MyPainter extends CustomPainter {
  BuildContext context;
  ui.Image? image;

  MyPainter({required this.context, required this.image});

  @override
  Future<void> paint(Canvas canvas, Size size) async {
    print("${size}");

    // 绘制蓝色矩形
    final paint = Paint()
      ..color = Colors.blue
      ..style = PaintingStyle.fill;

    final rect = Rect.fromLTWH(0, 0, size.width, size.height);
    // 绘制红色圆形
    final circlePaint = Paint()
      ..color = Colors.red
      ..style = PaintingStyle.fill;

    final center = Offset(size.width / 2, size.height / 2);
    canvas.drawCircle(center, 50, circlePaint);

    final paint2 = Paint()
      ..color = Colors.green
      ..strokeWidth = 10
      ..style = PaintingStyle.fill;
    canvas.drawLine(Offset(0, 0), Offset(100, 0), paint2);
    // final aa = await vg.loadPicture(SvgAssetLoader("assets/svg/ic_bike.svg"), context);

    // final aaa = await aa.picture.toImage(20, 20);
    if (image != null) {
      print("图片大小 ${image!.width}*${image!.height}");
      canvas.drawImageAuto(image!, width: size.width, height: size.height);
    }
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true; // 不需要重新绘制
  }
}
